package com.mvp.shq.washer.ui.forgetPWD;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.jaeger.library.StatusBarUtil;
import com.mvp.shq.washer.R;
import com.mvp.shq.washer.base.BaseActivity;
import com.mvp.shq.washer.utils.T;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ForgetPWDActivity extends BaseActivity<ForgetPWDPresenter, ForgetPWDModel> implements ForgetPWDConstract.View {


    @BindView(R.id.et_mobile)
    EditText etMobile;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.btn_code)
    Button btnCode;
    @BindView(R.id.et_password)
    EditText etPassword;
    private CountDownTimer timer;
    private final long TIME_SPACE = 1000;
    private final long ALL_TIME = 60 * TIME_SPACE;
    @Override
    public int getLayoutId() {
        return R.layout.activity_forget_pwd;
    }

    @Override
    public void initView() {
        StatusBarUtil.setColor(this, ContextCompat.getColor(context,R.color.white),0);
    }


    @OnClick({R.id.btn_code, R.id.btn_setpwd,R.id.back})
    public void onViewClicked(View view) {
        String mobile = etMobile.getText().toString();
        String code = etCode.getText().toString();
        String pwd = etPassword.getText().toString();
        switch (view.getId()) {
            case R.id.btn_code:
                if (TextUtils.isEmpty(mobile) || mobile.length()!=11){
                    T.show("请检查手机号码格式");
                    return;
                }
                btnCode.setBackgroundResource(R.drawable.code_btn_unclickable);
                btnCode.setText("获取中60s");
                btnCode.setClickable(false);
                timer = new CountDownTimer(ALL_TIME,TIME_SPACE) {
                    @Override
                    public void onTick(long millisUntilFinished) {
                        btnCode.setText("获取中" + millisUntilFinished/TIME_SPACE + "s");
                    }

                    @Override
                    public void onFinish() {
                        btnCode.setBackgroundResource(R.drawable.code_btn);
                        btnCode.setText("获取验证码");
                        btnCode.setClickable(true);
                    }
                };
                timer.start();
                presenter.code(mobile);
                break;
            case R.id.btn_setpwd:
                if (TextUtils.isEmpty(mobile) || mobile.length() != 11){
                    T.show("请检查手机号码格式");
                    return;
                }
                if (TextUtils.isEmpty(code) || code.equals("")){
                    T.show("请先填写验证码");
                    return;
                }
                if (TextUtils.isEmpty(pwd) || "".equals(pwd)){
                    T.show("请先输入密码");
                    return;
                }
                if (pwd.length() < 6){
                    T.show("密码长度不能小于6位");
                    return;
                }
                presenter.forget(mobile,code,pwd);
                break;
            case R.id.back:
                finish();
                break;
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timer != null){
            timer.cancel();
            timer = null;
        }
    }

    @Override
    public void rSuccess() {
        T.show("修改成功");
        finish();
    }

    @Override
    public void rFial(String msg) {
        T.show("msg");
    }

    @Override
    public void codeSuccess() {
        T.show("获取验证码成功");
    }

    @Override
    public void codeFail(String msg) {
        T.show(msg);
        btnCode.setBackgroundResource(R.drawable.code_btn);
        btnCode.setText("获取验证码");
        btnCode.setClickable(true);
    }
}
